package com.bsoft;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class Main {
    public static final String QUEUE_NAME = "queue_demo"; // 只有 QUEUE_NAME 需要共享给 RabbitMQConsumer
    private static final String IP_ADDRESS = "192.168.220.135";
    private static final Integer PORT = 5672;
    private static final String USERBNAME = "guest";
    private static final String PASSWOD = "guest";
    private static final String EXCHANGE_NAME = "exchange_demo";
    private static final String ROUTING_KEY = "routingkey_demo";

    public static void main(String[] args) throws Exception {
        mqConsumer();

    }

    public static void mqConsumer() throws IOException, TimeoutException, InterruptedException {
        Connection connection = getConnection();
        //创建信道
        Channel channel = connection.createChannel();
        channel.basicQos(64);  //设置客户端最多接收未被ack的消息数量为 64
        //创建消费者
        Consumer consumer = new DefaultConsumer(channel) {

            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                // 打印日志
                System.out.println(String.format("[线程：%s][路由键：%s][消息内容：%s]",
                        Thread.currentThread(), envelope.getRoutingKey(), new String(body)));
                // ack 消息已经消费
                channel.basicAck(envelope.getDeliveryTag(), false);
            }

        };
        //订阅消费 QUEUE_NAME 队列
        channel.basicConsume(QUEUE_NAME, consumer);

        //关闭
        TimeUnit.HOURS.sleep(1);
        channel.close();
        connection.close();
    }

    //创建连接
    public static Connection getConnection() throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(IP_ADDRESS);
        factory.setPort(PORT);
        factory.setUsername(USERBNAME);
        factory.setPassword(PASSWOD);
        return factory.newConnection();
    }

    private static void initExchangeAndQueue(Channel channel) throws IOException {
        //创建交互机 direct 持久化 不自动删除
        channel.exchangeDeclare(EXCHANGE_NAME, "direct", true, false, null);
        //创建队列 持久化 非排他 非自动删除的队列
        channel.queueDeclare(QUEUE_NAME, true, false, false, null);
        //将交互器与队列通过路由键绑定
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);
    }
    //创建Rabbitmq exchanges 和queue

    public void createmq() throws IOException, TimeoutException {
        //创建连接
        Connection connection = getConnection();
        //创建通道
        Channel channel = connection.createChannel();
        //初始化测试用的
        initExchangeAndQueue(channel);
        //发送三条数据
        for (int i = 0; i < 3; i++) {
            String message = "Hello Word" + i;
            channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY, MessageProperties.PERSISTENT_BASIC, message.getBytes());

        }
        //关闭
        channel.close();
        connection.close();
    }
}